package com.beijiteshop.shop.ui.order.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.ext.databinding.inflateBinding
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.DialogCouponSelectBinding
import com.beijiteshop.shop.model.api.response.Coupon
import com.beijiteshop.shop.model.entity.CheckBoxEntity
import com.beijiteshop.shop.ui.order.adapter.CouponSelectAdapter
import com.blankj.utilcode.util.ScreenUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/9 15:25
 * @Description 优惠券选择框
 */
class CouponSelectDialog : BottomSheetDialogFragment() {

    private lateinit var mBinding: DialogCouponSelectBinding
    private val mAdapter = CouponSelectAdapter(mutableListOf())
    private var mCheckCoupons = mutableListOf<CheckBoxEntity<Coupon>>()

    private var mSelectListener: IOnSelectListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.TransBgBottomSheet)
    }

    override fun onStart() {
        super.onStart()
        //设置默认高度
        val bottomSheet = dialog?.findViewById<FrameLayout>(R.id.design_bottom_sheet)
        bottomSheet?.let {
            it.layoutParams.height = ScreenUtils.getScreenHeight() * (4f/3f).toInt()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = inflateBinding(inflater)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRV()
        initEvent()
        mAdapter.setNewInstance(mCheckCoupons)
    }

    private fun initRV(){

        mAdapter.apply {
            setOnItemChildClickListener { _, _, position ->
                mSelectListener?.onSelect(position,data[position].data)
                dismiss()
            }
        }

        mBinding.couponSelectRv.apply {
            itemAnimator = null
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    private fun initEvent(){
        mBinding.couponSelectCloseIv.setOnClickListener {
            dismiss()
        }
    }

    /**
     * 优惠券选择回调
     */
    fun setOnSelectListener(listener: IOnSelectListener){
        mSelectListener = listener
    }

    fun show(fm: FragmentManager,data: MutableList<CheckBoxEntity<Coupon>>){
        updateData(data)
        show(fm,"")
    }

    private fun updateData(data: MutableList<CheckBoxEntity<Coupon>>){
        mCheckCoupons.clear()
        mCheckCoupons.addAll(data)
        mAdapter.setNewInstance(mCheckCoupons)
    }

    interface IOnSelectListener{
        fun onSelect(position: Int,coupon: Coupon)
    }

}